package com.client;

import com.model.Student;
import com.model.StudentMessage;

import java.io.*;
import java.util.*;

/**
 * 学员管理子系统
 */
public class StudentSystemView {
    private List<Student> students = new ArrayList<>();
    private ClientInitClose clientInitClose;
    private StudentMessage studentMessage;

    public StudentSystemView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    /**
     * 学员系统主界面
     */
    public void studentSystemManageMenu() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("          学员管理系统          ");
            System.out.println("------------------------------");
            System.out.println("[1] 增加学员         [2] 删除学员");
            System.out.println("[3] 修改学员         [4] 查找学员");
            System.out.println("[5] 遍历学员         [0] 退出   ");
            System.out.println("------------------------------");
            System.out.println("请输入您要操作的选项：");
            int option = ClientScanner.getScanner().nextInt();
            switch (option){
                case 0:
                    System.out.println("正在退出学员管理系统。。。。。。");
                    // 发送给服务器
                    clientInitClose.getObjectOutputStream().writeObject(new StudentMessage("quit",null));
                    clientInitClose.getObjectOutputStream().flush();
                    return;
                case 1:
                    //System.out.println("增加学员信息");
                    // 客户端定义要添加学员信息
                    System.out.println("请输入您要增加学员的id：");
                    String add_id = ClientScanner.getScanner().next();
                    System.out.println("请输入您要增加学员的姓名：");
                    String add_studentName = ClientScanner.getScanner().next();
                    System.out.println("请输入您要增加学员的年龄：");
                    int add_age = ClientScanner.getScanner().nextInt();
                    System.out.println("请输入您要增加学员的密码：");
                    String add_passWord = ClientScanner.getScanner().next();
                    Student add_student = new Student(add_id,add_studentName,add_age,add_passWord);
                    // 发送给服务器
                    clientInitClose.getObjectOutputStream().writeObject(new StudentMessage("add",add_student));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();
                    //System.out.println(studentMessage);
                    if (studentMessage.getMessage().equals("success")){
                        System.out.println("当前学生信息添加成功！");
                    }else
                        System.out.println("当前学员系统中已有该学员信息，无需再次添加！");
                    break;
                case 2:
                    //System.out.println("删除学员信息");
                    // 客户端定义要删除学员信息
                    System.out.println("请输入您要删除学员的id：");
                    String delete_id = ClientScanner.getScanner().next();
                    Student delete_student = new Student(delete_id,"",0,"");
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new StudentMessage("delete",delete_student));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();
                    if (studentMessage.getMessage().equals("success")){
                        System.out.println("删除学员信息成功！");
                    }else
                        System.out.println("输入学员id有误，系统中无该学员！");
                    break;
                case 3:
                    //System.out.println("修改学员信息");
                    // 客户端定义要修改学员信息
                    System.out.println("请输入您要修改学员的id：");
                    String modify_id = ClientScanner.getScanner().next();
                    System.out.println("请输入您要修改学员的姓名：");
                    String modify_studentName = ClientScanner.getScanner().next();
                    System.out.println("请输入您要修改学员的年龄：");
                    int modify_age = ClientScanner.getScanner().nextInt();
                    System.out.println("请输入您要修改学员的密码：");
                    String modify_passWord = ClientScanner.getScanner().next();
                    Student modify_student = new Student(modify_id,modify_studentName,modify_age,modify_passWord);
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new StudentMessage("modify",modify_student));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();
                    if (studentMessage.getMessage().equals("success")){
                        System.out.println("修改学员信息成功！");
                    }else
                        System.out.println("输入学员id有误，系统中无该学员！");

                    break;
                case 4:
                    //System.out.println("查找学员信息");
                    // 客户端定义要查找学员信息
                    System.out.println("请输入您要查找学员的id：");
                    String find_id = ClientScanner.getScanner().next();
                    Student find_student = new Student(find_id,"",0,"");
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new StudentMessage("find",find_student));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();

                    if (studentMessage.getMessage().equals("success")){
                        System.out.println("查找的学生信息如下：");
                        System.out.println(studentMessage.getStudent().toString());
                    }
                    else
                        System.out.println("输入学员id有误，系统中无该学员！");
                    break;
                case 5:
                    //System.out.println("遍历学员信息");
                    System.out.println("当前系统中的学员信息如下：");
                    // 发送给服务端
                    clientInitClose.getObjectOutputStream().writeObject(new StudentMessage("show",null));
                    clientInitClose.getObjectOutputStream().flush();
                    // 接收服务器端
                    studentMessage = (StudentMessage) clientInitClose.getObjectInputStream().readObject();
                    if (studentMessage.getMessage().equals("success")){
                        students = studentMessage.getStudents();
                        if (students.size() > 0){
                            for (Student student:students)
                                System.out.println(student.toString());
                        }else
                            System.out.println("当前系统中无学员信息！");
                    }else
                        System.out.println("您的输入有误，请重新输入！");
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }
}
